<?php
/**
 * 
 * @author Freezy
 *
 */
class RewardPunishment extends MY_Controller {

	function RewardPunishment(){
		parent::MY_Controller();
		$this->validate_cookie();
	}
	
	function index(){
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array('view'=>'template/horz_nav', 
											'view_data'=>array('data'=>$CONF['nav']['psdm']));
		$layoutConf['right_content'] = array('view'=>'rewardPunishment/overview', 
											'view_data'=>$CONF['search']);
		$this->initialize_layout($layoutConf);
		
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript_default'][] = 'psdm/rewardPunishment';
		$pageConf['jscript'] = $pageConf['jscript_default'];
		$pageConf['css'] = $pageConf['css_default'];
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();		
	}
	
	function _search(){
		$this->load->model('rewardPunishment_model');
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		$conf1 = $conf['rewardPunishment']['jenis_options'];
		
		//ada kodenya, jadi load config dulu
		$key = $this->input->post('key');
		$keysss = array();
		$keysss[] = $key; 
		if ($ss = array_search(ucwords($key), $conf1)) $keysss[] = $ss;	//key untuk jenis
		
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		
		$table_content = "";
		$row = 0;
		$data = $this->rewardPunishment_model->select_last_entry($keysss, false, $limit, $offset, $orderby);
		$table_content = $this->load->view('rewardPunishment/table_content', array('data'=>$data,'jenis_options'=>$conf1), true); 	
		echo json_encode(array(
			'table_content'=>$table_content,
			'table_num_record'=> $key? $data->num_rows() : $this->rewardPunishment_model->count_record()
		));
	}
	
	function _addModal(){
		$this->load->helper(array('html','url'));
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		
		$this->finalize_view_str(
			$this->load->view('rewardPunishment/add', array(
				'jenis_options'=>$conf['rewardPunishment']['jenis_options'],
			),true)
		);
	}
	
	function _save(){
		$this->load->model('rewardPunishment_model');
		$inp = $this->input->post('inp');
		
		//save
		$res = $this->rewardPunishment_model->insert( array(
			'reward_nama' => $inp[1],
			'reward_jenis' => $inp[0]
		));
		$msg = $res? "Proses simpan selsai" : "Proses simpan gagal, ".$this->rewardPunishment_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _editModal($id){
		$this->load->model('rewardPunishment_model');
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		
		$result = $this->rewardPunishment_model->select_by_id($id);
		$result = $result->row();
		$result = (array) $result;
		
		$arr = array(
			'jenis_options'=>$conf['rewardPunishment']['jenis_options'],
		);
		
		$this->finalize_view_str(
			$this->load->view('rewardPunishment/edit', array_merge($result, $arr), true)
		);
	}
	
	function _update(){
		$this->load->model('rewardPunishment_model');
		$inp = $this->input->post('inp');
		
		//save
		$res = $this->rewardPunishment_model->update(
			array('reward_id' => $inp[2]), 
			array(
				'reward_nama' => $inp[1],
				'reward_jenis' => $inp[0]
			));
		$msg = $res? "Proses update selesai" : "Proses update gagal, ".$this->rewardPunishment_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _delete($id){
		$this->load->model('detachievement_model');
		$res = $this->detachievement_model->delete(array('detachiev_id' => $id));
		$msg = $res? "Proses hapus selesai" : "Proses hapus gagal, ".$this->detachievement_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	/*****************************************
	 * MISC
	 *****************************************/
}
/* End of file psdm/rewardPunishment.php */
/* Location: ./system/application/controllers/psdm/rewardPunishment.php */